﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCSY.BLL;
using WCSY.MODEL;
using GoldMantis.Web.UI.WebControls;
using System.Data;
using System.Reflection;
using System.Collections;
using WCSY.DAL;


public partial class basedept_DeptList : System.Web.UI.Page
{
    Fg_deptInfoManager deptM = new Fg_deptInfoManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            
            //根据用户id获取权限集合
            //string userId = Session["userId"].ToString();
            //List<Mg_right> rights = tool.IsHasUserRights(userId);
            //foreach(Mg_right right in rights){
            //    if(right.Rightsid==255){
            //        //deptadd.Enabled = true;
                    
            //    }
            //    if(right.Rightsid==256){
            //        //deptupdate.Enabled = true;
            //    }
            //    if(right.Rightsid==257){
            //        //deptdel.Enabled = true;
                
            //    }
            //    if(right.Rightsid==258){
            //        //deptprt.Enabled = true;
                
            //    }
            
            //}

            initTree();
            DataTable deptInfo = deptM.GetAllList().Tables[0];
            myBind(deptInfo);
            //保存按钮不可用
            this.btnSave.Enabled = false;
            
        
        }

    }
    //绑定父目录
    public void initTree() {

        DataTable parentDt = deptM.getParentDeptList();
        deptTreeView.Nodes.Clear();
        TreeNode desc = new TreeNode();
        desc.Value = "0";
        desc.Text = "部门列表";
        deptTreeView.Nodes.Add(desc);
        foreach (DataRow row in parentDt.Rows)
        {
            TreeNode root = new TreeNode();
            string rootValue = row["deptName"].ToString();
            string rootId = row["id"].ToString();
            root.Text = rootValue;
            root.Value = rootId;


            desc.ChildNodes.Add(root);
            //root.ImageUrl = "~/image/base1.gif";
            //if (!t.Rightsurl.Equals(""))
            //{
            //    root.NavigateUrl = "JAVAscript:topage('../../" + t.Rightsurl + "');";
            //}
            
            childNodes(root);
        }
    
    }

    //绑定子目录
    public void childNodes(TreeNode childNode)
    {

        DataTable childDt = deptM.getChiledDeptList(childNode.Value);

        //childNode.SelectAction = TreeNodeSelectAction.Expand;

        foreach (DataRow row in childDt.Rows)
        {
            TreeNode child = new TreeNode();
            child.Text = row["deptName"].ToString();
            child.Value = row["id"].ToString();

            childNode.ChildNodes.Add(child);
            //child.ImageUrl = "~/image/base1.gif";
            //if (!c.Rightsurl.Equals(""))
            //{
            //    //child.NavigateUrl = "JAVAscript:openUrl('" + c.Rightsurl + "');";//href=\"javascript:topage('../../" + c.Rightsurl + "')\" 
            //    child.NavigateUrl = "JAVAscript:topage('../../" + c.Rightsurl + "');";
            //}
            //child.NavigateUrl = c.Rightsurl;

            childNodes(child);
        }


    }
    /// <summary>
    /// 绑定数据列表
    /// </summary>
    /// <param name="deptInfo"></param>
    public void myBind(DataTable deptInfo){
 
     this.deptListGridView.DataSource = deptInfo;

     this.deptListGridView.DataBind();
        
    
    }

   //删除数据
    protected void deptdel_Click(object sender, EventArgs e)
    {

        if (this.pageFlag.Value.Equals(""))
        {
            tool.Alert("没有选中数据行，请选择！", this);
            return;
        }
        List<string> sqlList = new List<string>();
        int id = Convert.ToInt32(this.pageFlag.Value);
        sqlList.Add(deptM.Delete(id));
        try
        {
            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);
            }
        }
        catch (Exception ex)
        {

            tool.Alert("信息删除失败!" + ex.Message, this);
            return;
        }
        DataTable dt = deptM.GetAllList().Tables[0];
        myBind(dt);
        tool.Alert("信息删除成功！",this);
       
      
    }
    //查询数据
    protected void secBtn_Click(object sender, EventArgs e)
    {

        //string deptNo = this.deptNoText.Text;
        //string deptName = this.deptNameText.Text;
        string key = this.keyword.Text.Trim();
        string where = "";
        if (!key.Equals(""))
        {
            where += " (deptno = '" + key + "' or deptname like '%" + key + "%')";
        }
       
        DataTable deptinfo = deptM.GetList(where).Tables[0];
        myBind(deptinfo);
       
    }
    //新增数据
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        
        string maxno = tool.GetBaseMaxSysno("fg_deptinfo");
        this.txtdeptNo.Text = maxno;
        //绑定部门信息
        this.ddlisEnd.Items.Clear();
        this.ddlisEnd.DataSource = deptM.GetAllList().Tables[0];
        this.ddlisEnd.DataValueField = "id";
        this.ddlisEnd.DataTextField = "deptName";
        this.ddlisEnd.DataBind();
        ListItem item = new ListItem();
        item.Value = "0";
        item.Text = "-请选择-";
        ddlisEnd.Items.Insert(0, item);
        
        txtdeptName.Text = "";
        txtmanager.Text = "";
        txtphone.Text = "";
        txtfax.Text = "";
        txtTelephone.Text = "";
        txtremarks.Text = "";
        this.pageFlag.Value = "";//设置页面状态为新增
        tool.SetControlReadOnlyNone(this);
        
        //设置按钮状态
        this.btnSave.Enabled = true;//保存
        this.btnUpd.Enabled = false;//修改
        this.btnDel.Enabled = false;//删除
        this.btnAdd.Enabled = false;//新建
        
       
    }
    //保存数据
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int maxid = tool.GetBaseMaxId("id","fg_deptinfo");
        string maxno = tool.GetBaseMaxSysno("fg_deptinfo");
        List<string> sqlList = new List<string>();
        try
        {
            Fg_deptInfo dept = new Fg_deptInfo();
            
            
            dept.deptName = txtdeptName.Text;
            dept.manager = txtmanager.Text;
            dept.phone = txtphone.Text;
            dept.fax = txtfax.Text;
            dept.telephone = txtTelephone.Text;
            dept.isEnd = Convert.ToInt32(ddlisEnd.SelectedValue);
            dept.remarks = txtremarks.Text;
            if(this.pageFlag.Value.Equals("")){//新增
                dept.id = maxid;
                dept.deptNo = maxno;
                sqlList.Add(deptM.Add(dept));
            }else{//修改
                dept.id = Convert.ToInt32(this.pageFlag.Value);
                dept.deptNo = txtdeptNo.Text;
                sqlList.Add(deptM.Update(dept));
            }
            
        }catch(Exception ex){
            tool.Alert("信息保存失败！" + ex.Message, this);
            return;
        
        }
        try
        {
            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);
            }
        }
        catch (Exception ex)
        {

            tool.Alert("信息保存失败!" + ex.Message, this);
            return;
        }
        //设置按钮状态
        this.btnSave.Enabled = false;//保存
        this.btnUpd.Enabled = true;//修改
        this.btnDel.Enabled = true;//删除
        this.btnAdd.Enabled = true;//新建
        tool.SetControlReadOnly(this);
        tool.SetControlReadOnlyNone(keyword);
        tool.SetControlReadOnlyNone(forkeyword);
        DataTable dt = deptM.GetAllList().Tables[0];
        myBind(dt);
        tool.Alert("信息保存成功！",this);      
        
    }
    //绑定行选定事件
    protected void deptListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header){
           e.Row.Cells[9].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
            e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click();");//selectx(this);
            
        }
        
    }
    //选中行绑定明细数据
    protected void deptListGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        int id = Convert.ToInt32(deptListGridView.SelectedDataKey[0]);
        this.pageFlag.Value = id.ToString();//隐藏控件pageFlag ,为判断当前为修改或是新增，如果为修改，则隐藏控件值不为空，为选中行id ,反之为新增状态。
        //绑定部门信息
        this.ddlisEnd.Items.Clear();
        this.ddlisEnd.DataSource = deptM.GetAllList().Tables[0];
        this.ddlisEnd.DataValueField = "id";
        this.ddlisEnd.DataTextField = "deptName";
        this.ddlisEnd.DataBind();
        ListItem item = new ListItem();
        item.Value = "0";
        item.Text = "-请选择-";
        ddlisEnd.Items.Insert(0, item);

        Fg_deptInfo dept = deptM.GetModel(id);
        txtdeptNo.Text = dept.deptNo;
        txtdeptName.Text = dept.deptName;
        txtmanager.Text = dept.manager;
        txtphone.Text = dept.phone;
        txtfax.Text = dept.fax;
        ddlisEnd.SelectedValue =dept.isEnd.ToString();
        txtTelephone.Text = dept.telephone;
        txtremarks.Text = dept.remarks;

        tool.SetControlReadOnly(this);
        tool.SetControlReadOnlyNone(keyword);
        tool.SetControlReadOnlyNone(forkeyword);
        //设置按钮状态
        this.btnSave.Enabled = false;//保存
        this.btnUpd.Enabled = true;//修改
        this.btnDel.Enabled = true;//删除
        this.btnAdd.Enabled = true;//新建
        
       
    }
    //修改按钮，修改控件编辑状态
    protected void btnUpd_Click(object sender, EventArgs e)
    {
        //设置所有控件取消只读状态
        tool.SetControlReadOnlyNone(this);
        //设置保存按钮可访问，并隐藏修改按钮
        this.btnSave.Enabled = true;//保存
        this.btnUpd.Enabled = false;//修改
        this.btnDel.Enabled = false;//删除
    }
    //选择节点后，绑定gridview数据
    protected void deptTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        string idvalue = deptTreeView.SelectedNode.Value;
        if (idvalue.Equals("0"))
        {
            DataTable dt = deptM.GetAllList().Tables[0];
            myBind(dt);
        }
        else {

            int id = Convert.ToInt32(idvalue);
            DataTable dt = deptM.GetList(" id = "+id+"").Tables[0];
            myBind(dt);
            //默认选中行
            deptListGridView.SelectedIndex = 0;
            //CommandEventArgs cm=new CommandEventArgs("choose",(deptListGridView.Rows[0].FindControl("LinkButton1") as LinkButton).CommandArgument);
            //GridViewCommandEventArgs arg=new GridViewCommandEventArgs(deptListGridView.Rows[0],cm);
            //deptListGridView_RowCommand(null, arg);

            deptListGridView_SelectedIndexChanged(sender, e);
        }
    }
}
